// 显示文章管理页面模块

// 引入Article集合规则
const Article = require('../../model/article.js');
// 引入分页查询模块
const pagination = require('mongoose-sex-page');

module.exports = async (req, res, next) => {
    req.app.locals.currentLink = 'article';
    let { page } = req.query;
    let limit = 5;
    try {
        // 查询最大页码
        let temp = await pagination(Article).page(1).size(limit).display(5).exec();
        let maxPage = temp.pages;
        // 页码判定
        page = page > 0 ? page > maxPage ? maxPage : page : 1;
        // 查询当前页码下的数据
        let result = await pagination(Article).find().page(page).size(limit).display(5).populate('writer').exec();
        result = JSON.stringify(result);
        result = JSON.parse(result);
        // 渲染用户管理页面
        res.render('admin/article', { data: result });
    } catch (error) {
        next(error);
    }
}